package com.xiaotu.spider.task.pageprocessor;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.xiaotu.common.model.CheckMovieInfoModel;
import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.PageUtils;
import com.xiaotu.common.util.UUIDUtils;
import com.xiaotu.server.service.check.CheckMaoYanMovieDataIdUpdateService;
import com.xiaotu.spider.AUrlPageProcessor;

import us.codecraft.webmagic.Page;

@Component("CheckMaoYanMovieDataIdUpdatePageProcessor")
public class CheckMaoYanMovieDataIdUpdatePageProcessor extends AUrlPageProcessor{

	
	protected String resourceSiteId = Constants.SiteName.MAOYAN;//数据来源
	
	
	@Autowired
	private CheckMaoYanMovieDataIdUpdateService checkMaoYanMovieDataIdUpdateService;
	
	
	private String [] START_URL = {
			"https://www.baidu.com/"
	};
	
	
	private static String BASICINFO_URL = "https://piaofang.maoyan.com/movie/%s?_v_=yes";//用于检查该剧是否已更新数据dataid
	
	
	@Override
	public void process(Page page) {
		
		Map<String, Object> extrasMap = page.getRequest().getExtras();
		
		switch (this.getPageDataType(page))
	    {
	        case Constants.PageDataType.BASICINFO:
	        	this.checkMaoYanMovieDataIdUpdateProcess(page,extrasMap);
	            break;
	        default:
	        	this.addRequestProcess(page);
	            break;
	    }
		
	}

	
	
	//检查是否已更新dataid
	private void checkMaoYanMovieDataIdUpdateProcess(Page page, Map<String, Object> extrasMap) {
		
		String oldTvName = extrasMap.get("tvName").toString();
		
		String oldDataId = extrasMap.get("dataId").toString();
		
		Integer oldTvType = Integer.parseInt(extrasMap.get("tvType").toString());
		
		String checkTvName = page.getHtml().xpath("//h1[@class='nav-header navBarTitle']//text()").get();
		
		if(StringUtils.isBlank(checkTvName)){
			
			
			//并且不存在该记录
			CheckMovieInfoModel param = new CheckMovieInfoModel();
			param.setDataId(oldDataId);
			param.setResourceSiteId(resourceSiteId);
			CheckMovieInfoModel result = checkMaoYanMovieDataIdUpdateService.queryMovieInfoById(param);
			if(null == result){
				CheckMovieInfoModel arriveYetAchievedCycleModel = new CheckMovieInfoModel(
						UUIDUtils.getId(), 
						oldDataId, 
						oldTvName, 
						oldTvType,
						resourceSiteId,
						null,
						System.currentTimeMillis()
						);
				
				page.putField(CheckMovieInfoModel.class.getName(), arriveYetAchievedCycleModel);
			}
			
		}
		
	}


	//添加任务
	private void addRequestProcess(Page page) {
		
		//检查猫眼剧id是否已改变
		List<CheckMovieInfoModel> list = checkMaoYanMovieDataIdUpdateService.queryMovieInfoList();
		
		for( CheckMovieInfoModel obj : list ){
			
			Map<String,Object> extraMap = new HashMap<String,Object>();
			extraMap.put("tvName", obj.getTvName());
			extraMap.put("dataId", obj.getDataId());
			extraMap.put("tvType", obj.getTvType());
			if(null == obj.getDataId() || StringUtils.isBlank(obj.getDataId())){
				continue;
			}
			PageUtils.addTargetRequest(page, String.format(BASICINFO_URL, obj.getDataId()), extraMap);
		}
		
		
	}

	@Override
	public String getPageDataType(Page page) {
		String url = page.getUrl().toString();
        if (url.contains("_v_=yes")){
        	return Constants.PageDataType.BASICINFO;
        }
        return Constants.PageDataType.SEARCH_LIST;
	}
	
	
	@Override
	protected String[] getStartRequestUrls() {
		return START_URL;
	}

	@Override
	protected String getSpiderType() {
		return Constants.SpiderTaskType.MOVIE_BOXOFFICE;
	}

}
